/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Collections.Specialized;
using System.Data;
using System.Web;
using System.Web.Security;

/// <summary>
/// Summary description for CustomMembershipProvider
/// </summary>
public class CustomMembershipProvider : MembershipProvider
{
	public CustomMembershipProvider()
	{
		//
		// TODO: Add constructor logic here
		//
	}

	public override string ApplicationName
	{
		get
		{
			throw new Exception("The method or operation is not implemented.");
		}
		set
		{
			throw new Exception("The method or operation is not implemented.");
		}
	}

	public override bool ChangePassword(string username, string oldPassword, string newPassword)
	{
		if (!ValidateUser(username, oldPassword))
			return false;

		bool bChanged = false;
		try
		{
            int n = Hipocrate.DB.DBConnection.Execute("Site_ChangePassword", new string[] { "@UserName", "@Parola" }, new object[] { username, newPassword });
			bChanged = (n > 0);
			Tools.ScrieInLog(HttpContext.Current.Server, string.Format("Login (ChangePassword) [SessionID='" + HttpContext.Current.Session.SessionID + "']: Utilizatorul '{0}' si-a schimbat parola.", username), Tools.LOG_INFO);
		}
		catch (Exception)
		{
			throw;
		}
		return bChanged;
	}

	public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override bool DeleteUser(string username, bool deleteAllRelatedData)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override bool EnablePasswordReset
	{
		get { throw new Exception("The method or operation is not implemented."); }
	}

	public override bool EnablePasswordRetrieval
	{
		get { throw new Exception("The method or operation is not implemented."); }
	}

	public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override int GetNumberOfUsersOnline()
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override string GetPassword(string username, string answer)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override MembershipUser GetUser(string username, bool userIsOnline)
	{
		MembershipUser u = new MembershipUser(this.Name, username, null, null, null, null, true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
		return u;
	}

	public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override string GetUserNameByEmail(string email)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override int MaxInvalidPasswordAttempts
	{
		get { throw new Exception("The method or operation is not implemented."); }
	}

	public override int MinRequiredNonAlphanumericCharacters
	{
		get { return 0; }
	}

	public override int MinRequiredPasswordLength
	{
		get { return 0; }
	}

	public override int PasswordAttemptWindow
	{
		get { throw new Exception("The method or operation is not implemented."); }
	}

	public override MembershipPasswordFormat PasswordFormat
	{
		get { throw new Exception("The method or operation is not implemented."); }
	}

	public override string PasswordStrengthRegularExpression
	{
		get { throw new Exception("The method or operation is not implemented."); }
	}

	public override bool RequiresQuestionAndAnswer
	{
		get { throw new Exception("The method or operation is not implemented."); }
	}

	public override bool RequiresUniqueEmail
	{
		get { throw new Exception("The method or operation is not implemented."); }
	}

	public override string ResetPassword(string username, string answer)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override bool UnlockUser(string userName)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override void UpdateUser(MembershipUser user)
	{
		throw new Exception("The method or operation is not implemented.");
	}

	public override void Initialize(string name, NameValueCollection config)
	{
		// Verify that config isn't null
		if (config == null)
			throw new ArgumentNullException("config");

		// Assign the provider a default name if it doesn't have one
		if (String.IsNullOrEmpty(name))
			name = "AspNetCustomMembershipProvider";

		// Add a default "description" attribute to config if the
		// attribute doesn't exist or is empty
		if (string.IsNullOrEmpty(config["description"]))
		{
			config.Remove("description");
			config.Add("description", "Custom SQL Provider");
		}

		// Call the base class's Initialize method
		base.Initialize(name, config);
	}

	public override bool ValidateUser(string username, string password)
	{
		string strUserID = Tools.GetUserID(username, password);
		if (strUserID != "")
			HttpContext.Current.Session["UserID"] = strUserID;
		return (strUserID != "");
	}

}
